//#define USE_OPENGL

using System;
using System.Collections.Generic;

using Reflexive.Game;
using Reflexive.Graphics;

using AGG;
using AGG.Image;
using AGG.VertexSource;
using AGG.UI;
using AGG.Transform;

using Tao.OpenGl;

namespace RockBlaster
{
    public class RockBlasterGame : Reflexive.Game.GamePlatform
    {
    	MainMenu m_MainMenu;
        HowManyPlayersMenu m_HowManyPlayersMenu;
        CreditsMenu m_CreditsMenu;
    	PlayfieldView m_Playfield;
        public RockBlasterGame(GuiHalSurface drawingSurface)
            : base(30, 5, drawingSurface)
        {
        }

        public void Initialize()
        {
            String PathToUse = "GameData";
            if (!System.IO.Directory.Exists(PathToUse))
            {
                PathToUse = "../../GameData";
                if (!System.IO.Directory.Exists(PathToUse))
                {
                    PathToUse = "../../../../RockBlaster/GameData";
                    if (!System.IO.Directory.Exists(PathToUse))
                    {
                    }
                }
            }

            DataAssetTree DataFolderTree = new DataAssetTree(PathToUse);
            DataAssetCache.Instance.SetAssetTree(DataFolderTree);

            Entity.GameWidth = (int)Width;
            Entity.GameHeight = (int)Height;

            m_MainMenu = new MainMenu(Bounds);
            AddChild(m_MainMenu);
            m_MainMenu.SendToBack();
            m_MainMenu.StartGame += new MainMenu.StartGameEventHandler(StartGame);
            m_MainMenu.ShowCredits +=new MainMenu.ShowCreditsEventHandler(ShowCredits);
            m_MainMenu.ExitGame += new MainMenu.ExitGameEventHandler(ExitGame);

            m_HowManyPlayersMenu = new HowManyPlayersMenu(Bounds);
            AddChild(m_HowManyPlayersMenu);
            m_HowManyPlayersMenu.SendToBack();
            m_HowManyPlayersMenu.StartOnePlayerGame += new HowManyPlayersMenu.StartOnePlayerGameEventHandler(StartOnePlayerGame);
            m_HowManyPlayersMenu.StartTwoPlayerGame += new HowManyPlayersMenu.StartTwoPlayerGameEventHandler(StartTwoPlayerGame);
            m_HowManyPlayersMenu.StartFourPlayerGame += new HowManyPlayersMenu.StartFourPlayerGameEventHandler(StartFourPlayerGame);
            m_HowManyPlayersMenu.CancelMenu += new HowManyPlayersMenu.CancelMenuEventHandler(BackToMainMenu);

            m_CreditsMenu = new CreditsMenu(Bounds);
            AddChild(m_CreditsMenu);
            m_CreditsMenu.SendToBack();
            m_CreditsMenu.CancelMenu += new CreditsMenu.CancelMenuEventHandler(BackToMainMenu);

            m_Playfield = new PlayfieldView(Bounds);
            AddChild(m_Playfield);
            m_Playfield.SendToBack();
            m_Playfield.Menu += new PlayfieldView.MenuEventHandler(EndGame);
            m_Playfield.Visible = false;

            MakeMenuVisibleHideOthers(m_MainMenu);
        }

        void MakeMenuVisibleHideOthers(GUIWidget whichWindow)
        {
            m_CreditsMenu.Visible = false;
            m_HowManyPlayersMenu.Visible = false;
            m_MainMenu.Visible = false;
            m_Playfield.Visible = false;

            whichWindow.Visible = true;
        }

        public void StartGame(GUIWidget widget)
        {
            MakeMenuVisibleHideOthers(m_HowManyPlayersMenu);
        }

        public void ShowCredits(GUIWidget widget)
        {
            MakeMenuVisibleHideOthers(m_CreditsMenu);
        }

        public void StartOnePlayerGame(GUIWidget widget)
        {
            m_Playfield.StartOnePlayerGame();
            MakeMenuVisibleHideOthers(m_Playfield);
        }

        public void StartTwoPlayerGame(GUIWidget widget)
        {
            m_Playfield.StartTwoPlayerGame();
            MakeMenuVisibleHideOthers(m_Playfield);
        }

        public void StartFourPlayerGame(GUIWidget widget)
        {
            m_Playfield.StartFourPlayerGame();
            MakeMenuVisibleHideOthers(m_Playfield);
        }

        public void BackToMainMenu(GUIWidget widget)
        {
            MakeMenuVisibleHideOthers(m_MainMenu);
        }

        public void ExitGame(GUIWidget widget)
        {
            guiHalSurface.Close();
        }
        
        public void EndGame(GUIWidget widget)
        {
            MakeMenuVisibleHideOthers(m_MainMenu);
        }

        public override void OnDraw(Graphics2D graphics2D)
        {
            this.ShowFrameRate = true;
            base.OnDraw(graphics2D);
        }

        public override void OnKeyDown(AGG.UI.KeyEventArgs keyEvent)
		{
        	if(keyEvent.KeyCode == Keys.F6)
        	{
        		// launch the editor
        	}
        	
			base.OnKeyDown(keyEvent);
		}

		public override void OnKeyUp(AGG.UI.KeyEventArgs keyEvent)
		{
			base.OnKeyUp(keyEvent);
		}

        bool haveInitialized = false;

        public override void OnUpdate(double NumSecondsPassed)
        {
            if (!haveInitialized)
            {
                haveInitialized = true;
                Initialize();
            }

        	if(m_Playfield.Visible)
        	{
        		m_Playfield.OnUpdate(NumSecondsPassed);
        	}
            
            base.OnUpdate(NumSecondsPassed);
        }

        public static void StartDemo()
        {
            GuiHalFactory.SetGuiBackend(GuiHalFactory.KnownGuiFactoriesIndexes.WindowsFormsBitmap);
            //GuiHalFactory.SetGuiBackend(new WindowsFormsOpenGLBackendGuiFactory());
            //GuiHalFactory.SetGuiBackend(new WindowsFormsDXBackendGuiFactory());
            GuiHalSurface primaryWindow = GuiHalFactory.CreatePrimarySurface(800, 600, GuiHalSurface.CreateFlags.Resizable, GuiHalSurface.PixelFormat.PixelFormatBgra32);

            primaryWindow.Caption = "Rock blaster is a game a lot like Asteroids.";

            primaryWindow.AddChild(new RockBlasterGame(primaryWindow));
            primaryWindow.Run();
        }

        [STAThread]
        public static void Main(string[] args)
        {
            StartDemo();
        }
    };
}
